package com.csu.client.module.messageProcess;


import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.swing.SwingUtilities;

import com.csu.client.logger.EIMLogger;
import com.csu.client.module.network.socket.ClientFileSocket;
import com.csu.client.module.vo.Cache;
import com.csu.message.BaseMessage;
import com.csu.message.FileRequestMessage;
import com.csu.message.FileResponseMessage;
import com.csu.message.FileSockectRegMessage;
import com.csu.message.MessageType;
import com.daohang361.imqq.ui.EIMTalkFrame;
import com.daohang361.imqq.ui.file.SendGetFileProgressPanle;

public class FileRequestProcess implements IMessageProcess,Runnable {

	private FileRequestMessage requestMessage;
	
	public void processMessage(BaseMessage message) {
		FileRequestMessage requestMessage=(FileRequestMessage)message;
		FileRequestProcess requestMessageProcess=new FileRequestProcess();
		requestMessageProcess.setRequestMessage(requestMessage);
	    try {
			SwingUtilities.invokeAndWait(requestMessageProcess);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} 
	}

	public void run() {
		
		 String friendQQ=requestMessage.getSrcQQ();
		 String owerQQ=requestMessage.getDestQQ();
		 Cache cache=Cache.getInstance();
		 EIMTalkFrame qqTalkFrame=cache.getTalkFrame(friendQQ);
		 if(!qqTalkFrame.isVisible()){
				qqTalkFrame.setVisible(true);
			}
		 qqTalkFrame.addFileJPanel(true, requestMessage);
		 
		 
		 
		 
		 
		
//// 		String msg="你是否接受"+requestMessage.getFileName()+" 大小为:"+requestMessage.getFileLength()/(1024*1024.0)+ "MB";
////		int result=javax.swing.JOptionPane.showConfirmDialog(qqTalkFrame,msg);
//		
//		FileResponseMessage responseMessage=new FileResponseMessage(owerQQ,friendQQ);
//		responseMessage.setFileLength(requestMessage.getFileLength());
//		responseMessage.setFileAbsolutePath(requestMessage.getFileAbsolutePath());
//		responseMessage.setFileName(requestMessage.getFileName());
//		
//		File file=new File("C:\\Documents and Settings\\Administrator\\My Documents"+"\\"+requestMessage.getFileName());
//		if(result==0){
//			
//			EIMLogger.INFO("此用户接受文件传输请求");
//			responseMessage.setFileResponse(MessageType.FileResponse_Accepted);
//			javax.swing.JFileChooser fileChooser = new javax.swing.JFileChooser();
//			fileChooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
//			fileChooser.showOpenDialog(qqTalkFrame);
//		    file = fileChooser.getCurrentDirectory();
//		    String saveFilePatn=file.getAbsolutePath()+"\\"+requestMessage.getFileName();
//		    responseMessage.setSaveFilePath(saveFilePatn);
//		    EIMLogger.Error("此用户保存的路劲为："+saveFilePatn);
//		    ClientFileSocket fileSocket=ClientFileSocket.getInstance();
//		    try {
//				fileSocket.startSocket();
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//		}else{
//			EIMLogger.INFO("此用户拒绝文件传输请求");
//			responseMessage.setSaveFilePath(file.getAbsolutePath());
//			responseMessage.setFileResponse(MessageType.FileResponse_Decline);
//		}
//		if(!file.exists()){
//			try {
//				file.createNewFile();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//		ClientSendFileMessageProcess fileMessageProcess=new ClientSendFileMessageProcess(null);
//		
//		ClientSendTextMessageProcess sendTextMessageProcess=ClientSendTextMessageProcess.getInstance();
//		sendTextMessageProcess.sendFileResponseMessage(responseMessage);
//		
//		if(responseMessage.getFileResponse()==MessageType.FileResponse_Accepted){
//			
//			EIMLogger.INFO("接收方 SendGetFileProgressPanle的ID为："+responseMessage.getSaveFilePath());
//			SendGetFileProgressPanle sendGetFileProgressPanle=new SendGetFileProgressPanle("get",responseMessage);
//			qqTalkFrame.addSendGetFileProcessPanle(sendGetFileProgressPanle);
//			sendGetFileProgressPanle.changeProcessgarValue(0);
//			try {
//				Thread.sleep(2000);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
//	        FileSockectRegMessage  fileSockectRegMessage=new  FileSockectRegMessage(owerQQ,MessageType.sysQQ);
//			fileMessageProcess.sendFileSockectRegMessage(fileSockectRegMessage);
//		}else{
//			file.delete();
//		}
//		fileMessageProcess=null;
	}

	public FileRequestMessage getRequestMessage() {
		return requestMessage;
	}

	public void setRequestMessage(FileRequestMessage requestMessage) {
		this.requestMessage = requestMessage;
	}

}
